package com.xxw.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.xxw.yygh.cmn.listener.DictListener;
import com.xxw.yygh.cmn.service.DictService;
import com.xxw.yygh.model.cmn.Dict;
import com.xxw.yygh.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxw.yygh.cmn.dao.DictDao;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 组织架构表(Dict)表服务实现类
 *
 * @author makejava
 * @since 2023-02-21 16:39:36
 */
@Service("dictService")
public class DictServiceImpl extends ServiceImpl<DictDao, Dict> implements DictService {

    @Resource
    private DictDao dictDao;

    //列表展示
    @Override
    @Cacheable(value = "abc",keyGenerator = "keyGenerator")
    public List<Dict> getChildListByPid(Long pid) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",pid);
        List<Dict> dicts = baseMapper.selectList(queryWrapper);
        for (Dict dict : dicts) {
            //查询是否有子元素
            dict.setHasChildren(isHasChildren(dict.getId()));
        }
        return dicts;
    }

    //文件下载
    @Override
    public void download(HttpServletResponse response) throws IOException {
        List<Dict> list = baseMapper.selectList(null);
        ArrayList<DictEeVo> dictEeVoList = new ArrayList<>(list.size());
        for (Dict dict : list) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictEeVoList.add(dictEeVo);
        }
        //下载：响应头信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码 当然和easy excel没有关系
        String fileName = URLEncoder.encode("字典文件", "utf-8");
        response.setHeader("Content-disposition","attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet("学生列表1").doWrite(dictEeVoList);
    }

    //文件上传
    @Override
    @CacheEvict(value = "abc",allEntries = true)
    public void upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(dictDao)).sheet(0).doRead();
    }

    @Override
    public String getNameByValue(Long value) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("value",value);

        Dict dict = baseMapper.selectOne(dictQueryWrapper);
        if (dict != null) {
            return dict.getName();
        }
        return null;
    }

    @Override
    public String getNameByDictCodeAndValue(String dictCode, Long value) {
        //第一次查询
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("dict_code",dictCode);
        Dict dict = baseMapper.selectOne(dictQueryWrapper);

        QueryWrapper<Dict> dictQueryWrapper2 = new QueryWrapper<>();
        dictQueryWrapper2.eq("parent_id",dict.getId());
        dictQueryWrapper2.eq("value",value);
        Dict dict2 = baseMapper.selectOne(dictQueryWrapper2);

        return dict2.getName();
    }

    private boolean isHasChildren(Long pid) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",pid);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count > 0;
    }
}

